﻿using System;
using System.Text;

namespace DroidPad
{
    class connHandler
    {
        private bluetoothConn blueconn;
        private wifiConn wificonn;
        private Delegate gotDataCallback;
        private Delegate gotInitDataCallback;
        private Delegate onStopCallback;
        private ConnType connectionType;
        public enum ConnType
        {
            None,
            WiFi,
            Bluetooth,
        }

        public connHandler(Delegate func1,Delegate func2,Delegate func3)
        {
            gotInitDataCallback = func1;
            gotDataCallback = func2;
            onStopCallback = func3;
            blueconn = new bluetoothConn(new gotDataDelegate(gotData), new connectionClosedDelegate(connectionClosed));
            wificonn = new wifiConn(new gotDataDelegate(gotData), new connectionClosedDelegate(connectionClosed));
        }

        public void start()
        {
            connectionType = ConnType.None;
            blueconn.start();
            wificonn.start();
        }

        public void close()
        {
            switch (connectionType)
            {
                case ConnType.WiFi:
                    wificonn.close();
                    break;
                case ConnType.Bluetooth:
                    blueconn.close();
                    break;
                case ConnType.None:
                    wificonn.close();
                    blueconn.close();
                    break;
            }
        }

        public void writeData(byte[] data, int size)
        {
            switch (connectionType)
            {
                case ConnType.WiFi:
                    wificonn.writeData(data, size);
                    break;
                case ConnType.Bluetooth:
                    blueconn.writeData(data, size);
                    break;
            }
        }

        private delegate void gotDataDelegate(ConnType connType, String data);
        private void gotData(ConnType connType,String data)
        {
            if (connectionType == ConnType.None)
            {
                if ((bool)gotInitDataCallback.DynamicInvoke(data))
                {
                    connectionType = connType;
                    switch (connType)
                    {
                        case ConnType.Bluetooth:
                            wificonn.close();
                            break;
                        case ConnType.WiFi:
                            blueconn.close();
                            break;
                    }
                }
                return;
            }
            gotDataCallback.DynamicInvoke(data);
        }

        private delegate void connectionClosedDelegate();
        private void connectionClosed()
        {
            switch (connectionType)
            {
                case ConnType.Bluetooth:
                    blueconn.stop();
                    break;
                case ConnType.WiFi:
                    wificonn.stop();
                    break;
                case ConnType.None: return;
            }
            onStopCallback.DynamicInvoke();
            start();
        }
    }
}